package ctgu;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class PhoneTotal {
    private final int  msConversion = 1000; //毫秒与秒转换
    private final int  minuteConversion = 60; //秒与分钟转换
    private final int timeAdjust = 60; //时间转换差值
    private final int timeLimit = 20; //通话时间界限
    private final double levelOneCharge = 0.05; //通话时间小于等于20分钟时，每分钟收费0.05美元
    private final double levelTwoCharge = 0.1; //通话时间大于20分钟的部分，每分钟收费0.1美元
    private final int overOneMinuteCharge = 60000; //通话时间超过一分钟的时间转换单位
    private final int maxMinutes = 1800; //通话时间不超过1800分钟
    private final int hourtime1 = 2; //两点
    private final int hourtime2 = 3; //三点
    private final int lastday = 25;

    public long timeCalculate(final String startDate, final String endDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date start = format.parse(startDate);
        Date end = format.parse(endDate);
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(start);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(end);
        long phonetime = cal2.getTime().getTime() - cal1.getTime().getTime();
        long minute;
        if (phonetime >= 0 && phonetime / msConversion < minuteConversion || phonetime % overOneMinuteCharge > 0) {
            //不到一分钟按一分钟计算
            minute = phonetime / msConversion / minuteConversion + 1;
        } else {
            minute = phonetime / msConversion / minuteConversion;
        }
        if (cal1.get(Calendar.MONTH) == Calendar.NOVEMBER && cal1.get(Calendar.DATE) >= lastday
                && cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            //开始时间在十一月最后一个周日
            if (cal1.get(Calendar.HOUR_OF_DAY) == hourtime1 && cal2.get(Calendar.HOUR_OF_DAY) == hourtime1
                    && cal2.get(Calendar.HOUR_OF_DAY) < hourtime2 && cal2.get(Calendar.MINUTE)
                    <= cal1.get(Calendar.MINUTE)) {
                //因为转换而出现通话时间为负的情况
                minute += timeAdjust;
            } else if (cal1.get(Calendar.HOUR_OF_DAY) < hourtime1 && cal2.get(Calendar.HOUR_OF_DAY) >= hourtime1) {
                //开始时间是两点前，结束时间是两点后（经历第二次转换）
                minute = minute + timeAdjust;
            }
        }

        if ((cal1.get(Calendar.MONTH) == Calendar.MARCH && cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
            //开始时间在三月周日
            if (cal1.get(Calendar.HOUR_OF_DAY) < hourtime1 && cal2.get(Calendar.HOUR_OF_DAY) >= hourtime2) {
                //开始时间在两点前，结束时间在三点后（经过了第一次转换）
                minute -= timeAdjust;
            }
        } else { //开始时间不在三月周日，但是结束时间在（从前一天打到这一天 )
            if ((cal2.get(Calendar.MONTH) == Calendar.MARCH && cal2.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
                if (cal2.get(Calendar.HOUR_OF_DAY) >= hourtime2) {
                    minute -= timeAdjust;
                }
            }
        }
        return minute;
    }
    public String moneyCalculate(final long minute) {
        if (minute <= 0 || minute > maxMinutes) {
            return "通话时间错误";
        }
        double money;
        if (minute <= timeLimit) {
            money = minute * levelOneCharge;
        } else {
            money = 1 + (minute - timeLimit) * levelTwoCharge;
        }

        return String.valueOf(String.format("%f %n", money));
    }
}
